﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Student.Models;
using PagedList;

namespace OT.Areas.Student.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Student/Account/
        #region Declartion
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        #endregion

        #region Index
        public ActionResult Index()
        {
             int UserID=0;
             if (Request.Cookies["LoginSesID"] != null)
             {
                 UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
             }
             else
             {
                 TempData["notice"] = "Time Out ,Please Login";
                 Response.Redirect("~/Home");
             }
            Users Users = null;
            try
            {
                var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
                if (User != null)
                {
                    Users = new Users();
                    Users.UserID = User.UserID;
                    Users.FirstName = User.FirstName;
                    Users.LastName = User.LastName;
                    Users.EmailAddress = User.EmailAddress;
                    Users.Address = User.Address;
                    Users.MobileNo = User.MobileNo;
                    Users.LandlineNo = User.LandlineNo;
                    Users.CreatedDate = (DateTime)User.CreatedDate;
                    Users.UpdatedDate = (DateTime)User.UpdatedDate;
                    Users.LastLoginDate = (DateTime)User.LastLoginDate;
                    Users.CountryID = (int)User.CountryID;
                    Users.StateID = (int)User.StateID;
                    Users.CityID = (int)User.CityID;
                    var Country = OTDb.Countries.ToList();
                    Users.Countrys = Country;
                    var State = OTDb.States.Where(s => s.CountryID == (int)User.CountryID).ToList();
                    Users.States = State;
                    var City = OTDb.Cities.Where(s => s.StateID == (int)User.StateID).ToList();
                    Users.Citys = City;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Users);
        }

        [HttpPost]
        public ActionResult Index(Users Users)
        {
            try
            {
                int UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
                if (ModelState.IsValid)
                {
                    var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
                    if (User != null)
                    {
                        var Country = OTDb.Countries.ToList();
                        Users.Countrys = Country;
                        var Stats = OTDb.States.ToList();
                        Users.States = Stats;
                        var City = OTDb.Cities.ToList();
                        Users.Citys = City;
                        User.FirstName = Users.FirstName;
                        User.LastName = Users.LastName;
                        User.EmailAddress = Users.EmailAddress;
                        User.Address = Users.Address;
                        User.MobileNo = Users.MobileNo;
                        User.LandlineNo = Users.LandlineNo;
                        User.UpdatedDate = DateTime.Now;
                        User.CountryID = (int)Users.CountryID;
                        User.StateID = (int)Users.StateID;
                        User.CityID = (int)Users.CityID;
                        OTDb.SaveChanges();
                        TempData["notice"] = "Successfully Update";
                        return RedirectToAction("Index", "Account");
                    }
                }
                else
                {

                    Users = new Users();
                    var Country = OTDb.Countries.ToList();
                    Users.Countrys = Country;
                    var Stats = OTDb.States.ToList();
                    Users.States = Stats;
                    var City = OTDb.Cities.ToList();
                    Users.Citys = City;
                }

            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Users);
        }
     
        #endregion
        //
        // POST: /Admin/User/Edit/5
        #region Update
        [HttpPost]
        public string Change(string FirstName, string LastName, string Address, string MobileNo, string LandlineNo, int CountryID, int StateID, int CityID)
        {
            string result = "";
            int UserID = 0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                 UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
           
            var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
            if (User != null)
            {
                User.FirstName = FirstName;
                User.LastName = LastName;
                User.Address = Address;
                User.MobileNo = MobileNo;
                User.LandlineNo = LandlineNo;
                User.UpdatedDate = DateTime.Now;
                User.CountryID = (int)CountryID;
                User.StateID = (int)StateID;
                User.CityID = (int)CityID;
                OTDb.SaveChanges();
                result = "valid";
                TempData["notice"] = "Successfully Update";
            }
            }
            return result;
        }
        #endregion

        #region Jsoncall
        private IList<State> GetState(int CountryID)
        {
            return OTDb.States.Where(m => m.CountryID == CountryID).ToList();
        }

        private IList<City> GetCity(int StateID)
        {
            return OTDb.Cities.Where(c => c.StateID == StateID).ToList();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadState(string CountryID)
        {
            if (CountryID != "")
            {
                var stateList = this.GetState(Convert.ToInt32(CountryID));
                var stateData = stateList.Select(m => new SelectListItem()
                {
                    Text = m.StateName,
                    Value = m.StateID.ToString(),
                });
                return Json(stateData, JsonRequestBehavior.AllowGet);
            }
            return Json(CountryID);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadCity(string StateID)
        {
            var CityList = this.GetCity(Convert.ToInt32(StateID));
            var CityData = CityList.Select(c => new SelectListItem()
            {
                Text = c.CityName,
                Value = c.CityID.ToString(),
            });
            return Json(CityData, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
